package subject_set.interview.Day11_19_cider;

/**
 * @author haomin
 * @date 2022/11/19 11:47
 **/

//去除链表当中连续重复的元素【1,4,4,5,5,5,6】 -> [1,6]
class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}

public class Interview {
    public static ListNode delete(ListNode head) {
        if(head == null) return head;
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode cur = dummyHead.next;
        while(cur != null) {
            ListNode next = cur.next;
            if(next == null) return dummyHead.next;
            else {
                if(cur.val == next.val) {
                    while(next != null && cur.val == next.val){
                        next = next.next;
                    }
                    pre.next = next;
                    cur = next;
                }else {
                    pre = cur;
                    cur = next;
                }
            }
        }
        return dummyHead.next;
    }
    //去除链表当中连续重复的元素【1,4,4,5,5,5,6】 -> [1,6]
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(4);
        head.next = new ListNode(4);
        head.next = new ListNode(5);
        head.next = new ListNode(5);
        head.next = new ListNode(5);
        head.next = new ListNode(6);
        ListNode delete = delete(head);
        while(delete != null) {
            System.out.println(delete.val);
            delete = delete.next;
        }
    }
}